﻿using System.Security.Claims;
//
namespace Sustainalytics.Utils
{
    public static class ApiControllerHelper
    {
        static public string GetCurrentUserName(this  System.Web.Http.ApiController ctrl)
        {
            var principal = ctrl.User as ClaimsPrincipal;
            if (principal == null || principal.Identity == null || principal.Identity.Name == null)            
                throw CustomExceptionBuilder.CreateUserNotAuthenticated();
            
            return principal.Identity.Name;
        }

        static public ClaimsPrincipal GetCurrentUserClaims(this System.Web.Http.ApiController ctrl)
        {
            var principal = ctrl.User as ClaimsPrincipal;

            if (principal == null || principal.Identity == null || principal.Identity.Name == null)
                throw CustomExceptionBuilder.CreateUserNotAuthenticated();

            return principal;
        }
    }
}
